package th.ac.tu.engr.timemylife;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.PowerManager;

import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.database.sqlite.SQLiteDatabase;
import android.content.ContentValues;
import android.util.Log;


public class TickReceiver extends BroadcastReceiver {
    private static final String TAG = "TickReceiver";
    private static int[] activeStatus = new int[60];
    private static int activeSum =0;
    private static int workTime = 480;
    private static int restTime = 480;


    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: load settings from preference
        // TODO: check screen status from PowerManager
        PowerManager pm = (PowerManager)context.getSystemService(context.POWER_SERVICE);
        Calendar rightNow = Calendar.getInstance();
        int date = rightNow.get(Calendar.DAY_OF_WEEK);
        int hour = rightNow.get(Calendar.HOUR_OF_DAY);
        int minute = rightNow.get(Calendar.MINUTE);
        if (pm.isScreenOn()) {
            // TODO: update data - screen on

            activeStatus[minute] = 1;
            activeSum++;

            Log.v(TAG, "activeStatus = " + activeStatus[minute]);
            Log.v(TAG, "activeSum = " + activeSum );

            if(hour == 8 || hour ==9 || hour ==10 || hour ==11 || hour ==12 ||
                    hour ==13 || hour ==14 || hour ==15 || hour ==16) {

                workTime--;

            }



            if(hour == 22 || hour ==23 || hour ==0 || hour ==1 || hour ==2 ||
                    hour ==3 || hour ==4 || hour ==5 || hour ==6) {

                restTime--;

            }


        } else {
            // TODO: update data - screen off

            activeStatus[minute] = 0;
            Log.v(TAG, "activeStatus = " + activeStatus[minute]);
            Log.v(TAG, "activeSum = " + activeSum );
        }

        // Update database at xx:59
        if (minute == 59) {
            //UsageEntry.update

            UsageEntry.update(rightNow,activeSum);
            activeSum=0;

        }
        // Sync database at 23:59
        if (minute==59 && hour ==23) {
            //UsageEntry.query
            //Connect to server

            activeSum=0;
            String value = UsageEntry.query(rightNow);

            SharedPreferences pref = MyApp.getAppPreference();
            int ID = pref.getInt("ID",0);

            try {

                Log.v(TAG, "Inside try Before ServerAsyncTask");

                ServerAsyncTask registerTask = new ServerAsyncTask();

                URL url = new URL("http://time-my-life.appspot.com/update?id=" + ID + "&values=" + value );
                registerTask.execute(url);


            } catch (MalformedURLException e) {
                e.printStackTrace();
            }


        }

        //Create row at 00.00
        if (minute==00 && hour ==00){

           // UsageEntry.addRow
            UsageEntry.addDate(rightNow);

        }


    }
}
